﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;

namespace MensUnoShop.Areas.Admin.Models
{
    public abstract class MensUnoImport
    {
        private bool _issave;
        protected HttpPostedFileBase _file;
        protected string _filepath;
        protected ImportType _it;
        protected SenerPro.Logger _log;

        public bool ISSAVE { set { _issave = value; } get { return _issave; } }
        public MensUnoImport() { }
        public MensUnoImport(HttpPostedFileBase file, ImportType it)
        {
            _issave = false;
            _filepath = string.Empty;
            _file = file;
            _it = it;
            _log = new SenerPro.Logger(MensUnoShop.Models.Config.logpath);
            _issave = SaveFile();
        }

        protected bool SaveFile()
        {
            try
            {
                if (_file != null && _file.ContentLength > 0)
                {
                    string path = MensUnoShop.Models.Config.uploadpath;
                    string filenameExtesion = Path.GetExtension(_file.FileName).ToLower().Replace(".", "");
                    if ((new string[] { "jpg", "gif", "png", "bmp", "csv", "xls", "xlsx" }).Contains(filenameExtesion))
                    {
                        string filename = Guid.NewGuid().ToString("N");
                        _filepath = Path.Combine(path, filename + "." + filenameExtesion);
                        _file.SaveAs(_filepath);
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "SaveFile", ex.ToString());
            }
            return false;
        }

    }

    public enum ImportType
    {
        Product = 1,
        Picture,
        Tag
    }
}